<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户id" prop="userId">
        <el-select
          v-model="queryParams.userId"
          placeholder="请选择用户"
          style="width: 240px"
        >
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['comment:comment:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="commentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评价id" align="center" prop="id" />
      <el-table-column label="用户" align="center" prop="userId">
        <template #default="scope">
          {{ findUserById(scope.row.userId) }}
        </template>
      </el-table-column>
      <el-table-column label="评论内容" align="center" prop="contant" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Comment">
import {
  listComment,
  getComment,
  delComment,
  addComment,
  updateComment,
} from "@/api/comment/comment";
import { listDishes } from "@/api/dishes/dishes";
import { listUser } from "@/api/system/user";

const { proxy } = getCurrentInstance();

const commentList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    contant: null,
    createTime: null,
    createAt: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

const userList = ref([]);


//获取用户列表
const getUserList = () => {
  listUser().then((response) => {
    userList.value = response.rows.filter((v) => v.roleIds[0] === 2);
  });
};

// 根据id查询用户
const findUserById = (id) => {
  console.log(id, userList.value);
  
  const current = userList.value.filter((v) => v.userId === id)[0];
  console.log(current);

  return current?.userName;
};

getUserList();

/** 查询评价管理列表 */
function getList() {
  loading.value = true;
  listComment(queryParams.value).then((response) => {
    commentList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    contant: null,
    createTime: null,
    createAt: null,
  };
  proxy.resetForm("commentRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加评价管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getComment(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改评价管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["commentRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateComment(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addComment(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除评价管理编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delComment(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "comment/comment/export",
    {
      ...queryParams.value,
    },
    `comment_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
